package com.lebang.controller.sys.cences;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.CencesAdRecordLog;
import com.lebang.domain.TaskRouse;
import com.lebang.example.CencesAdRecordLogExample;
import com.lebang.feign.AppDictFeign;
import com.lebang.model.AppDictModel;
import com.lebang.model.RequestUserInfo;
import com.lebang.service.NewRouseService;
import com.lebang.service.cences.CencesAdRecordLogService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * @author ljx
 * @date 2020-12-08
 */
@Controller
@RequestMapping("/sys/cencesadrecordlog")
public class CencesAdRecordLogController {
    @Autowired
    private CencesAdRecordLogService baseService;
    @Autowired
    private NewRouseService newRouseService;
    @Autowired
    private AppDictFeign feign;

    @Login
    @GetMapping("/list")
    public String list(Model model, HttpSession session) {
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("item", rouses);
        return "/cencesadrecordlog/list";
    }

    @Login
    @GetMapping("/add")
    public String add(Integer id, Model model) {
        CencesAdRecordLog vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new CencesAdRecordLog();
        }
        model.addAttribute("item", vo);
        return "/cencesadrecordlog/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(CencesAdRecordLog vo) {
        if (vo.getId() != null) {
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer time) {
        baseService.deleteByTime(time);
        return Callback.success("success", "操作成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit,String sdkKey,Integer star, Integer end) {
        CencesAdRecordLogExample example = new CencesAdRecordLogExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        example.setOrderByClause("time desc,id desc");
        CencesAdRecordLogExample.Criteria criteria = example.createCriteria();
        if (star != null && end != null) {
            criteria.andTimeBetween(star, end);
        }
        if (!TextUtil.isEmpty(sdkKey)){
            criteria.andSdkKeyEqualTo(sdkKey);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }

    @Login
    @PostMapping("/lastCheck")
    @ResponseBody
    public String cen(HttpServletRequest request, String date) {
        RequestUserInfo userInfo=new RequestUserInfo(request);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end=LocalDate.parse(date, fmt);
        Integer value=null;
        try {
            JsonData jsonData = feign.get("ad_max_count", userInfo.getAppId());
            if(jsonData.isSuccess()){
                AppDictModel appDictModel = JSONObject.parseObject(JSONObject.toJSONString(jsonData.getData()), AppDictModel.class);
                value=Integer.parseInt(appDictModel.getKeyvalue());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        baseService.starDataInit(LocalDateTime.of(end.plusDays(1), LocalTime.MIN),value);
        return "success";
    }
}
